/**
 * @Description 公用接口抽离
 * @Date 2020-9-03
 */
import { fxhyApi,exMoney,newMyBalance,syncToken } from '@/api/index'
import { getCookie, setCookie, loginOutAndClearInfo,setItem,getItem } from '@/util'
import { ProductGroup, ExchangeID } from '@/api/enmu'

/**
 * @description 获取合约列表
 * @param params 请求参数
 */
export const getContractListFun = (params = {}) => {
    return new Promise((resolve,reject) => {
        let getContractStatus = getItem('contractAll')
        let {pageSize = 10,pageIndex = 1, ProductGroup = 'Swap'} = params || {}
        fxhyApi.getContractListLink({pageSize,pageIndex,ProductGroup }).then(res => {
            let data = res?.data || []
            if(res && res.data) {
                setItem('contractList',JSON.stringify(res))
            }
            if(!getContractStatus) {
                data = data.filter(i => i.SortNumber >= 0)
            }
            resolve(data)
        })
    })
}

/**
 * @description 获取持仓单列表
 * @param params 请求参数
 */
export const getPositionListFun = (params) => {
    return new Promise((resolve,reject) => {
        params['ExchangeID'] = ExchangeID
        params['pageIndex'] = '1'
        params['pageSize'] ='10'
        params['ProductGroup'] = ProductGroup
        // params['TradeUnitID'] = params.MemberID
        fxhyApi.positionOrder(params).then(res => {
            resolve(res) 
        })
    })
    
}

/**
 * @description 获取合约账户信息
 * @param params 请求参数
 */
export const getAccountPromis = (currencyName) => {
    return new Promise(resolve => {
        fxhyApi.account({currency: currencyName }).then(res => {
            let { retCode = -1, retData } = res || {}
            retData = !retData ? {} : retData
            if (retCode === 0 && retData.data) {
                let dataList = retData.data
                if (dataList.length) {
                    let accountObject = dataList.find(i => i.Currency == currencyName);
                    resolve(accountObject)
                } else {
                    resolve({})
                }
            }
        })
    })
}

/**
 * @description 获取体验金金额
 */
export const exMoneyApi = () => {
    return new Promise((resolve,reject) => {
        exMoney({currency: 'BTC'}).then(res => {
            let { retData = -1, retCode } = res || {}
            if(retCode == 0 && retData) {
                resolve(retData)
            }
        })
    })
}

/**
 * @description 获取资产信息
 */
export const getMyBalanceApi = () => {
    return new Promise((resolve,reject) =>{
        newMyBalance().then(res => {
            let { retCode = -1, retData = {} } = res || {}
            if(retCode == 0 && retData) {
                resolve(retData)
            } else {
                reject(res)
            }
        })
    })
}


/**
 * @description 同步token 信息到交易系统
 */
export const syncTokenFun = () => {
    return new Promise((resolve, reject) => {
        syncToken().then(res => {
            let {retCode = -1, retData } = res || {}
            if(retCode == 0) {
                setCookie('Otoken', getCookie('token'))
                resolve(true)
            } else {
                loginOutAndClearInfo('','','/register?status=login',false)
                resolve(false)
            }
        })
    })
}

